package com.example.everying.service.official.impl;/*
 * 项目名称: everything
 * 文件名: OfficialNotificationServiceImpl
 * 作者: 张泽阳
 * 日期: 2025/6/19
 * 描述:
 */

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.everying.context.UserContext;
import com.example.everying.mapper.official.OfficialNotificationMapper;
import com.example.everying.model.dto.official.OfficialNotificationDto;
import com.example.everying.model.entity.official.OfficialNotification;
import com.example.everying.model.entity.system.SysNotification;
import com.example.everying.model.vo.user.LoginUser;
import com.example.everying.model.vo.user.UserInfoVo;
import com.example.everying.service.official.OfficialNotificationService;
import com.example.everying.service.user.UserService;
import com.example.everying.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

@Service
public class OfficialNotificationServiceImpl extends ServiceImpl<OfficialNotificationMapper, OfficialNotification> implements OfficialNotificationService {

    @Autowired
    private UserService userService;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public List<SysNotification> listByTypeAndStatus(Byte type, Byte status) {
        return List.of();
    }

    @Override
    public boolean saveNotification(OfficialNotification notification) {
        notification.setCreateTime(LocalDateTime.now());
        notification.setUpdateTime(LocalDateTime.now());
        return save(notification);
    }

    @Override
    public boolean updateNotificationById(OfficialNotification notification) {
        notification.setUpdateTime(LocalDateTime.now());
        return updateById(notification);
    }

    @Override
    public boolean deleteNotificationById(Long id) {
        OfficialNotification notification = getById(id);
        if (notification != null) {
            notification.setIsDelete(true);
        }
        return updateById(notification);
    }

    @Override
    public IPage<OfficialNotification> selectPage(Page<OfficialNotification> pageParam, OfficialNotificationDto dto) {
        UserInfoVo user = UserContext.getUser();
        QueryWrapper<OfficialNotification> queryWrapper = new QueryWrapper<>();
        if (dto.getTitle() != null && !dto.getTitle().isEmpty()){
            queryWrapper.like("title", dto.getTitle());
        }
        queryWrapper.eq("is_delete",0);
        if (!(user.getIsAdministrator() == 1)){
            queryWrapper.eq("status", "1");
        }

        queryWrapper.orderByDesc("create_time");

        if (dto.getStartTime() != null && dto.getEndTime() != null) {
            queryWrapper.between("create_time", dto.getStartTime(), dto.getEndTime());
        }
        // 未读记录清零
        UserInfoVo userById = userService.getUserById(UserContext.getUser().getId());
        userById.setNoticeUnreadCount(0);
        userById.setUpdateTime(LocalDateTime.now());
        userService.updateById(userById);
        return baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public Result<?> cancelOfficialNotice(Long id) {
        OfficialNotification notification = baseMapper.selectById(id);
        if (notification == null){
            return Result.fail("通知不存在");
        }
        if (!notification.getStatus().equals("1")){
            return Result.fail("通知状态错误");
        }
        notification.setStatus("2");
        notification.setUpdateTime(LocalDateTime.now());
        boolean update = updateById(notification);
        if ( update){
            return Result.ok(update);
        }
        return Result.fail("取消通知失败");
    }
}
